package com.eshopkeeper.provider.sys;

import com.baomidou.mybatisplus.plugins.Page;
import com.eshopkeeper.core.base.BaseProviderImpl;
import com.eshopkeeper.core.support.login.ThirdPartyUser;
import com.eshopkeeper.core.util.SecurityUtil;
import com.eshopkeeper.dao.sys.SysUserMapper;
import com.eshopkeeper.dao.sys.SysUserMenuMapper;
import com.eshopkeeper.dao.sys.SysUserRoleMapper;
import com.eshopkeeper.model.sys.SysUser;
import com.eshopkeeper.model.sys.SysUserThirdparty;
import com.eshopkeeper.model.sys.ext.SysUserBean;
import com.weibo.api.motan.config.springsupport.annotation.MotanService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * SysUser服务实现类
 * @author chenshufeng
 * @version 2017-08-27 22:39:42
 */
//@CacheConfig(cacheNames = "SysUser")
@MotanService(basicService = "serviceBasicConfig")
public class SysUserProviderImpl extends BaseProviderImpl<SysUser> implements ISysUserProvider {
	
    @Autowired
    private SysUserMenuMapper thirdpartyMapper;
    
    //@Autowired
    //private ISysDicProvider sysDicProvider;
    
    //@Autowired
    //private ISysDeptProvider sysDeptProvider;
    
    @Autowired
    private SysUserMapper sysUserMapper;
    
    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    public Page<SysUser> query(Map<String, Object> params) {
        Page<Long> page = this.getPage(params);
        page.setRecords(mapper.selectIdByMap(page, params));
        return getPage(page);
    }

    public Page<SysUserBean> queryBeans(Map<String, Object> params) {
        Page<Long> idPage = this.getPage(params);
        List<Long> ids = mapper.selectIdByMap(idPage, params);
        Page<SysUserBean> pageInfo = new Page<SysUserBean>(idPage.getCurrent(), idPage.getSize());
        pageInfo.setTotal(idPage.getTotal());

        if (ids.size() > 0) {
        	List<SysUserBean> sysUserList = sysUserMapper.selectSysUserByMap(ids);
        	pageInfo.setRecords(sysUserList);
        } else {
        	pageInfo.setRecords(null);
        }
        return pageInfo;
    }

    /** 查询第三方帐号用户Id */
    @Cacheable
    public Long queryUserIdByThirdParty(String openId, String provider) {
        return thirdpartyMapper.queryUserIdByThirdParty(provider, openId);
    }

    // 加密
    public String encryptPassword(String password) {
        return SecurityUtil.encryptMd5(SecurityUtil.encryptSHA(password));
    }

    /** 保存第三方帐号 */
    @Transactional
    public SysUser insertThirdPartyUser(ThirdPartyUser thirdPartyUser) {
        SysUser sysUser = new SysUser();
        sysUser.setSex(0);
        sysUser.setUserType(1);
        sysUser.setPassword(this.encryptPassword("123456"));
        sysUser.setUserName(thirdPartyUser.getUserName());
        sysUser.setAvatar(thirdPartyUser.getAvatarUrl());
        // 初始化第三方信息
        SysUserThirdparty thirdparty = new SysUserThirdparty();
        thirdparty.setProvider(thirdPartyUser.getProvider());
        thirdparty.setOpenId(thirdPartyUser.getOpenid());
        thirdparty.setCreateTime(new Date());

        this.update(sysUser);
        sysUser.setAccount(thirdparty.getProvider() + sysUser.getId());
        this.update(sysUser);
        thirdparty.setUserId(sysUser.getId());
        thirdpartyMapper.insert(thirdparty);
        return sysUser;
    }

    @Override
    public  List<SysUserBean>  getRole(Long id) {
        return sysUserMapper.getRole(id);
    }

    public void init() {
//        List<Long> list = sysUserMapper.selectIdByMap(Collections.<String, Object>emptyMap());
//        for (Long id : list) {
//            RedisUtil.set(getCacheKey(id), mapper.selectById(id));
//        }
    }
    
    @Transactional
    public int insertSysUser(SysUser sysUser) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("account",sysUser.getAccount());
        String userName=sysUserMapper.getUserName(paramMap);
        if(StringUtils.isNotEmpty(userName)){
         return -1;
        }

    	int count = sysUserMapper.insertSysUser(sysUser);
        if (count>0) {
            sysUserRoleMapper.insertSysRoleUser(sysUser);
        }
        return count;
    }

	@Override
	public void updateSysUser(Map<String, Object> paramMap) {
		sysUserMapper.updateSysUser(paramMap);
	}

    @Override
    public String getUserName(Map<String, Object> params) {

        return sysUserMapper.getUserName(params);
    }

    @Override
    public int updateUserRole(SysUser sysUser) {
       return sysUserMapper.updateUserRole(sysUser);
    }
    @Override
    public void updateFace(Map<String, Object> paramMap) {
        sysUserMapper.updateFace(paramMap);
    }

    @Override
    public int resetPassword(SysUser sysUser) {
        return sysUserMapper.resetPassword(sysUser);
    }

    @Override
    public SysUser queryUserById(Long id) {
        return sysUserMapper.queryUserById(id);
    }

	@Override
	public List<Map<String, Object>> getUserList() throws Exception {
		return sysUserMapper.getUserList();
	}

    /**
     * Title:查询用户的名称和id
     * @param params
     * @return
     * @throws Exception
     */
    @Override
    public List<Map<String, Object>> getCheckUser(List<Long> params) throws Exception {
        //userId 去重
        Set set = new HashSet();
        List<Long> newuserIds = new ArrayList<>();
        set.addAll(params);
        newuserIds.addAll(set);
        List<Map<String, Object>> resultList=new ArrayList<>();
        if(newuserIds.size()>0){
            resultList= sysUserMapper.getCheckUser(newuserIds);
        }
        return resultList;
    }

}
